package com.lovo.ssc.controller;


import com.lovo.dto.PendingCommodityDto;
import com.lovo.ssc.bean.Commoditydescribe;
import com.lovo.ssc.bean.Commodityimg;
import com.lovo.ssc.bean.Pendingcommodity;
import com.lovo.ssc.service.ICommoditydescribeService;
import com.lovo.ssc.service.ICommodityimgService;
import com.lovo.ssc.service.IPendingcommodityService;
import com.lovo.util.MQUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-11
 */
@RestController
@RequestMapping("/pendingcommodity")
@Api(tags = "待上架商品应用控制器--------刘文龙")
@Slf4j
public class PendingcommodityController {


	@Autowired
	public RabbitTemplate rabbitTemplate;

	@Autowired
	IPendingcommodityService pendingcommodityService;
	@Autowired
	ICommodityimgService imgService;
	@Autowired
	ICommoditydescribeService describeService;

	@Scheduled(cron = "0 0 0 ? * *")
	@ApiOperation(value = "定时调度拉取待上架商品")
	public void conservetPendingcommodity(){
//		//等待后台系统完善接口
//		List<PendingcommodityDto> pendingcommodityDtoList = new ArrayList<>();
//		//将待上架商品持久化到本地
//		for (PendingcommodityDto p:pendingcommodityDtoList) {
//			Pendingcommodity pendingcommodity = new Pendingcommodity();
//			//对象拷贝
//			pendingcommodity.setCommoditycode(p.getCommodityCode());
//			pendingcommodity.setCommodityname(p.getCommodityname());
//			pendingcommodity.setNorms(p.getNorms());
//			pendingcommodity.setType(p.getType());
//			pendingcommodity.setNumber(p.getNumber());
//			pendingcommodity.setPrice(p.getPrice());
//			//保存到本地
//			pendingcommodityService.save(pendingcommodity);
//		}

	}

	@ApiOperation(value = "从本地获取待上架商品")
	@GetMapping("getPendingcommodity")
	public List<Pendingcommodity> getPendingcommodity(){
		return pendingcommodityService.list();
	}


	@ApiOperation(value = "编辑待上架商品")
	@PutMapping("sendPendingCommodityToBackstage/{commodityCode}/{imgPath}/{describe}/{price}")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "commodityCode", value = "商品编号"),
			@ApiImplicitParam(name = "imgPath", value = "商品图片路径"),
			@ApiImplicitParam(name = "describe", value = "商品描述"),
			@ApiImplicitParam(name = "price", value = "价格")
	})
	public void sendPendingCommodityToBackstage(@PathVariable("commodityCode") String commodityCode,
	                                            @PathVariable("imgPath") String imgPath,
	                                            @PathVariable("describe") String describe,
	                                            @PathVariable("price") int price){
		//添加商品图片
		Commodityimg img = new Commodityimg();
		img.setFkcommoditycode(commodityCode);
		img.setImgpath(imgPath);
		imgService.save(img);

		//添加商品描述
		Commoditydescribe commoditydescribe = new Commoditydescribe();
		commoditydescribe.setCommoditycode(commodityCode);
		commoditydescribe.setCommoditydescribe(describe);
		describeService.save(commoditydescribe);

		//向后台系统发送编辑后的待上架商品信息
		//根据商品编号得到待上架商品信息
		Pendingcommodity p = pendingcommodityService.findByCommodityCode(commodityCode);
		//将价格改为销售价格
		p.setPrice(price);
		//将p转化为PendingCommodityDto
		PendingCommodityDto pendingcommodityDto = new PendingCommodityDto();
		pendingcommodityDto.setCommodityCode(p.getCommoditycode());
		pendingcommodityDto.setCommodityname(p.getCommodityname());
		pendingcommodityDto.setNorms(p.getNorms());
		pendingcommodityDto.setType(p.getType());
		pendingcommodityDto.setNumber(p.getNumber());
		pendingcommodityDto.setPrice(p.getPrice());
		//通过MQ，将pendingcommodityDto发送给商城后台系统
		rabbitTemplate.convertAndSend(MQUtil.pendingCommodityFanoutExchange,MQUtil.pendingCommodityQueue,pendingcommodityDto);
	}


}
